<?php


namespace app\user\controller;


use app\index\controller\Base;
use app\user\model\EmrRecords;
use app\index\service\QrcodeServer;
use app\user\model\User as UserModel;
class Emr extends Base
{
    public function get(){
        $res = new EmrRecords();
        $data=$this->request->param();
        $d=$res->with("EmrImg,user")
            ->where($data['field'],$data['op'],$data['value'])
            ->select();
        return $d;
    }

    /*获取emr列表ermLists*/
    public function emrLists(){
        $data=$this->request->param();
        $emr = new EmrRecords();
        $lists=$emr->where('u_id',$data['u_id'])
            ->order('cTime desc')
            ->select();
        $this->assign('list',$lists);
        return $this -> fetch('index@index/item');
    }
    //详情页面
    public function record()
    {
        return $this ->fetch('index@index/record');
    }

    /*获取最新的病历码图片*/
    public function getEmrImage()
    {
        $data=$this->request->param();
        $emr = new EmrRecords();
        $code_img=$emr->where('u_id',$data['u_id'])->value('code_img');
        return $code_img;

    }
    public function update()
    {
        $data = $this->request->param();
        $res = new EmrRecords();
        return $res->updateModel($data);
    }
    public function delete(){
        $data = $this->request->param();
        $res = new EmrRecords();
        return $res->deleteModel($data);
    }
    public function save(){
        $res = new EmrRecords();
        $data = $this->request->param();
        $d= $res->saveModel($data);
        return $d->er_id;
    }
    public function num(){
        $data = $this->request->param();
        $res =new EmrRecords();
        return $res->where($data['field'],$data['op'],$data['value'])->count();
    }
    /*个人的病历列表*/
    public function createEmrImg(){
        $data = $this->request->param();
        $host=config("host_public");
        // 自定义二维码配置
        $config = [
            'title'         => false,
//            'title_content' => '病历码'.time(),
            'logo'          => true,
            'logo_url'      => $data['url'],
            'logo_size'     => 80,
            "generate"      =>"writefile",
            "file_name"     =>"static/sharehis/QrCode/"
        ];
        $qr_url = $host.'/user/emr/emrLists?u_id='.$data['u_id'];
        $qr_code = new QrcodeServer($config);
        $rs = $qr_code->createServer($qr_url);
        $new =new UserModel();
        $new->update(['u_id' => $data['u_id'], 'emrcode_img' => $rs['data']['url']]);
        return $rs['data']['url'];
    }
}